import Reflux from 'reflux';
import API from 'config/api';
import StateMixin from 'reflux-state-mixin';
import $ from 'jquery'

import CatalogueAction from 'pages/catalogue/actions/catalogueAction';


export default Reflux.createStore({
  mixins:[StateMixin.store],
  listenables:[CatalogueAction],
  getInitialState() {
    return {
      popServicesList:null,
      servicesList:null,
      allServices:null
    }
  },
  onGetAllServices(data) {
    $.ajax({
      url: API.GET_ALL_SERVICES,
      traditional: true,
      data: data || {}
    })
        .done((data) => {
          this.setState({
            allServices: data.data.list
          });
          CatalogueAction.getAllServices.completed(data.data);
        })
        .fail((error) => {
          CatalogueAction.getAllServices.failed(error);
        })
  },
  onGetPopServiceItem() {
    $.ajax({
      url: API.GET_POP_SERVICE_ITEM,
      traditional: true,
      data:  {}
    })
            .done((data) => {
              this.setState({
                popServicesList: data.data.list
              });
              CatalogueAction.getPopServiceItem.completed(data.data);
            })
            .fail((error) => {
              CatalogueAction.getPopServiceItem.failed(error);
            })
  },
  onGetServicesList(data) {
    $.ajax({
      url: API.GET_SERVICES_LIST,
      traditional: true,
      data: data || {}
    })
      .done((data) => {
        this.setState({
          servicesList: data.data
        });
        CatalogueAction.getServicesList.completed(data.data);
      })
      .fail((error) => {
        CatalogueAction.getServicesList.failed(error);
      })
  },
  onGetSubServices(data) {
    let param = {};
    param.pageSize = 9;
    param.curPage = 0;
    $.ajax({
      url:API.GET_SUB_SERVICES,
      traditional:true,
      data:data || param
    })
        .done((data)=>{
          CatalogueAction.getSubServices.completed(data.data);
        })
        .fail((error) => {
          CatalogueAction.getServicesList.failed(error);
        })
  },
  onGetTicket() {
    window.location =
        'http://10.1.60.251/itsm/#/create/6b7d35f79a2141c7bd4b69f32a110f05/xxxx?ticketSource=live&return_insite=' +
        encodeURIComponent('http://x.uyundev1.cn:8080/#/catalogue')

  }
})
